﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Smled
{
    public class RandomBrush
    {
        private List<Tile> allTiles = new List<Tile>();
        private string name = "New brush";
        private INIGroup iniDesc = null;
        private int[] ids = null;

        public RandomBrush()
        {
            RandomBrushStorage.Brushes.Add(this);
        }

        public RandomBrush(string theName)
        {
            this.name = theName;
            RandomBrushStorage.Brushes.Add(this);
        }

        public string Name { get { return name; } set { name = value; } }

        public override string ToString()
        {
            return name;
        }

        public bool Contains(Tile t)
        {
            return allTiles.Contains(t);
        }

        public void LoadFromFile(string fileName, TileSet ts)
        {
            LoadFromFile(new FileStream(fileName, FileMode.Open), ts);
        }

        public void LoadFromFile(FileStream fs, TileSet ts)
        {
            INIFile ini = new INIFile(fs);
            INIGroup g = null;
            while ((g = ini.getNext()) != null)
            {
                if (g.title == "RandomBrush")
                {
                    iniDesc = g;
                    ReadFromSource();
                    FillIn(ts);
                }
            }
        }

        public void SaveToFile(string fileName)
        {
            UpdateSource();
            iniDesc.AppendToFile(fileName);
        }

        private void ReadFromSource()
        {
            name = iniDesc.getString("Name", "New brush");
            ids = iniDesc.getIntList("Tiles", -1);
        }

        public void FillIn(TileSet ts)
        {
            if(allTiles == null)
                allTiles = new List<Tile>();
            else
                allTiles.Clear();

            foreach (int i in ids)
            {
                if (i >= 0)
                    allTiles.Add(ts.GetTile(i));
            }
        }

        private void UpdateSource()
        {
            if (iniDesc == null) iniDesc = new INIGroup("RandomBrush");
            iniDesc.set("Name", name);
            
            string list = "";
            for (int i = 0; i < allTiles.Count; ++i)
            {
                list += allTiles[i].ID.ToString();
                if (i < allTiles.Count - 1) list += ";";
            }
            iniDesc.set("Tiles", list);
        }

        public Tile GetTile()
        {
            return allTiles[Util.getRnd().Next(0, allTiles.Count)];
        }

        public void Paint(TileMap map, int x, int y)
        {
            map.SetTileAt(x, y, GetTile());
        }

        public void PaintToMapView(MapView mv, int x, int y)
        {
            mv.PaintTile(x, y, GetTile());
        }

        public List<Tile> Tiles { get { return allTiles; } }
    }

    static class RandomBrushStorage
    {
        private static List<RandomBrush> brushes = new List<RandomBrush>();

        public static List<RandomBrush> Brushes { get { return brushes; } }
    }
}
